﻿/*
** Haaf's Game Engine 1.6
** Copyright (C) 2003-2006, Relish Games
** hge.relishgames.com
**
** hge_tut03 - Using helper classes
*/



// Copy the files "particles.png", "menu.wav",
// "font1.fnt", "font1.png" and "trail.psi" from
// the folder "precompiled" to the folder with
// executable file. Also copy hge.dll and bass.dll
// to the same folder.


import core.hgeall;


import std.c.math;


// Pointer to the HGE interface.
// Helper classes require this to work.
ifHge	hge;


// Pointers to the HGE objects we will use
hgeSprite			spr;
hgeSprite			spt;
hgeFontCN			fnt;
hgeParticleSystem	par;

// Handles for HGE resourcces
HTEXTURE			tex;
HEFFECT				snd;

// Some "gameplay" variables
float x=100.0f, y=100.0f;
float dx=0.0f, dy=0.0f;

const float speed=90.0f;
const float friction=0.98f;

// Play sound effect
void boom() {
    int pan=cast(int)((x-400)/4);
    float pitch=(dx*dx+dy*dy)*0.0005f+0.2f;
    hge.Effect_PlayEx(snd,100,pan,pitch);
}


extern(C) {
    bool FrameFunc() {
        float dt=hge.Timer_GetDelta();

        // Process keys
        if (hge.Input_GetKeyState(HGEK_ESCAPE)) return true;
        if (hge.Input_GetKeyState(HGEK_LEFT)) dx-=speed*dt;
        if (hge.Input_GetKeyState(HGEK_RIGHT)) dx+=speed*dt;
        if (hge.Input_GetKeyState(HGEK_UP)) dy-=speed*dt;
        if (hge.Input_GetKeyState(HGEK_DOWN)) dy+=speed*dt;

        // Do some movement calculations and collision detection
        dx*=friction;
        dy*=friction;
        x+=dx;
        y+=dy;
        if (x>784) {
            x=784-(x-784);
            dx=-dx;
            boom();
        }
        if (x<16) {
            x=16+16-x;
            dx=-dx;
            boom();
        }
        if (y>584) {
            y=584-(y-584);
            dy=-dy;
            boom();
        }
        if (y<16) {
            y=16+16-y;
            dy=-dy;
            boom();
        }

        // Update particle system
       par.info.nEmission=cast(int)(dx*dx+dy*dy)*2;
        par.MoveTo(x,y);
        par.Update(dt);

        return false;
    }


    bool RenderFunc() {
        // Render graphics
        hge.Gfx_BeginScene();
        hge.Gfx_Clear(0);
        par.Render();
        spr.Render(x, y);
        fnt.printf(5, 5, HGETEXT_LEFT, "dt:%.3f\nFPS:%d (constant)", hge.Timer_GetDelta(), hge.Timer_GetFPS());
        hge.Gfx_EndScene();

        return false;
    }

}

void main() {
    gameInfo gi= gameInfo.getInstance();
    hge= gi.hge();

    gi.setVideo(640,480);
    gi.setFunc(&FrameFunc,&RenderFunc);
    gi.setStr("me","me.log");
	gi.setFps(100);
    try {

        gi.init();

        // Load sound and texture
        snd=hge.Effect_Load(r"res\menu.wav");

        HTEXTURE 	tex=gi.res.tex_load( r"res\particles.png");
        if (!snd) {
            // If one of the data files is not found, display
            // an error message and shutdown.
            msgbox("Can't load one of the following files:\nMENU.WAV, PARTICLES.PNG, FONT1.FNT, FONT1.PNG, TRAIL.PSI", "Error");
            throw new Exception("载入 snd 失败!");
        }

        // Create and set up a sprite
        spr=new hgeSprite(tex, 96, 64, 32, 32);
        spr.SetColor(0xFFFFA000);
        spr.SetHotSpot(16,16);

        // Load a font
        fnt=new hgeFontCN(r"..\test3\font_cn.sbin");

        // Create and set up a particle system
        spt=new hgeSprite(tex, 32, 32, 32, 32);
        spt.SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE);
        spt.SetHotSpot(16,16);
        par=new hgeParticleSystem(r"res\trail.psi",spt);

        par.Fire();

        hge.System_Start();
    } catch (Exception e) {
        mes("主程异常:	",e);
    } finally {

        delete par;
        hge.Effect_Free(snd);
        delete gi;

    }
}